package com.example.twitter;

import java.util.ArrayList;
import java.util.List;

import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.TwitterException;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.example.twitter.*;


public class TimeLine extends Activity implements OnItemClickListener{
	
	List<Status> statuses;
	ConfiguracionTwitter config;
	ListView listaTweets;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.timeline);	
		listaTweets = (ListView) this.findViewById(R.id.listaTw);
		listaTweets.setOnItemClickListener(this);
		ArrayList<TweetPerson> myArray = new ArrayList<TweetPerson>();
		
		
		Bundle b = this.getIntent().getExtras();
		
		config = new ConfiguracionTwitter(this);
		SharedPreferences prefs = this.getSharedPreferences("MySettingsTwitter", 0);
		config.token = prefs.getString("token", "");
		config.tokenSecret = prefs.getString("tokenSecret", "");
		config.accessToken = new AccessToken(config.token, config.tokenSecret);
		
		config.twitter.setOAuthAccessToken(config.accessToken);
		
		statuses = null;
		
		try {
			Paging paging = new Paging(1,30);
			statuses = config.twitter.getHomeTimeline(paging);
		} catch (TwitterException e) {
			
			e.printStackTrace();
		}
		
		
		myArray = this.ObtenerTweets();	
		TweetAdapter adapter = new TweetAdapter(this, myArray);
		
		
		listaTweets.setAdapter(adapter);
		
		
	}
	
	private ArrayList<TweetPerson> ObtenerTweets(){
		ArrayList<TweetPerson> myArray = new ArrayList<TweetPerson>();
		TweetPerson tweet = null;
		int contador = 0;
		for(Status status : statuses){
			tweet = new TweetPerson(status.getText(), status.getUser(), contador);
			myArray.add(tweet);
			contador++;
		}
		
		return myArray;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		menu.add(Menu.NONE, 1, Menu.NONE, "Escribir Tweet");
		menu.add(Menu.NONE, 2, Menu.NONE, "Log off");
		menu.add(Menu.NONE, 3, Menu.NONE, "Mi perfil");
		menu.add(Menu.NONE, 4, Menu.NONE, "Actualizar");
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		if(item.getItemId() == 1){
			// Escribir tweet
			Intent intent = new Intent(this, CrearTweet.class);
			startActivityForResult(intent, 0);
			
			
			
		}
		else if(item.getItemId() == 2){
			config.BorrarCredenciales(this.getApplicationContext());
			Intent intent = new Intent(this, Main.class);
			startActivity(intent);
			this.finish();
			
		}
		else if(item.getItemId() == 3){
			Intent intent = new Intent(this, Perfil.class);
			
			try {
				intent.putExtra("id", config.twitter.getId());
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (TwitterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			startActivity(intent);
		}
		else if(item.getItemId()==4){
			ActualizarTimeLine();
		}
		return true;
	}
	
	private void ActualizarTimeLine(){
		ArrayList<TweetPerson> myArray = new ArrayList<TweetPerson>();
		
		try {
			Paging paging = new Paging(1,30);
			statuses = config.twitter.getHomeTimeline(paging);
		} catch (TwitterException e) {
			
			e.printStackTrace();
		}
		
		
		myArray = this.ObtenerTweets();	
		TweetAdapter adapter = new TweetAdapter(this, myArray);
		
		
		listaTweets.setAdapter(adapter);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent){
		Toast toast = null;
		if(resultCode == RESULT_OK){
			toast = Toast.makeText(this.getApplicationContext(), "Tweet enviado con exito", Toast.LENGTH_LONG);
			toast.show();
		}
		else if(resultCode == RESULT_FIRST_USER){
			
				toast = Toast.makeText(this.getApplicationContext(), "Error en el envío del tweet", Toast.LENGTH_LONG);
				toast.show();
			
		}
		
		
		
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(this.getApplicationContext(), Tweet.class);
		
		intent.putExtra("id", statuses.get(arg2).getId());
		this.startActivity(intent);
		
	}

		
}